/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.interpreter.frontends.owlxml;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Laurent WOUTERS
 */
public class StatementLoader {
    private static final String attrVariable = "variable";

    public static xowl.lang.actions.Statement load(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        String name = node.getNodeName();
        if (name.equals(xowl.interpreter.frontends.VocActions.stAdd)) return load_Add(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stRemove)) return load_Remove(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stRemoveAll)) return load_RemoveAll(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stAssign)) return load_Assign(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stBreak)) return load_Break(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stContinue)) return load_Continue(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stInvoke)) return load_Invoke(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stExecute)) return load_Execute(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stBlock)) return load_Block(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stIf)) return load_If(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stFor)) return load_For(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stForEach)) return load_ForEach(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stWhile)) return load_While(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stDeclare)) return load_Declare(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocActions.stReturn)) return load_Return(ontologyLoader, node, context);
        if (name.equals(xowl.interpreter.frontends.VocInterop.stJavaInvokation)) return load_JavaInvokation(ontologyLoader, node, context);
        return null;
    }

    private static xowl.lang.actions.Statement load_Add(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.actions.Add statement = new xowl.lang.actions.Add();
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        for (org.w3c.dom.Node child : children)
            statement.addAxioms(AxiomLoader.load(ontologyLoader, child, context));
        return statement;
    }
    private static xowl.lang.actions.Statement load_Remove(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.actions.Remove statement = new xowl.lang.actions.Remove();
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        for (org.w3c.dom.Node child : children)
            statement.addAxioms(AxiomLoader.load(ontologyLoader, child, context));
        return statement;
    }
    private static xowl.lang.actions.Statement load_RemoveAll(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.actions.RemoveAll statement = new xowl.lang.actions.RemoveAll();
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        statement.setAxioms(AxiomLoader.load(ontologyLoader, children.get(0), context));
        return statement;
    }
    private static xowl.lang.actions.Statement load_Assign(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.actions.Assign statement = new xowl.lang.actions.Assign();
        statement.setVariable(context.resolveCode(node.getAttributes().getNamedItem(attrVariable).getNodeValue()));
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        if (children.size() > 1) {
            if (ExpressionLoader.isToSeqNode(children.get(0))) {
                statement.setLiteralSeq(ExpressionLoader.load_ToSeq(ontologyLoader, children.get(0), context));
            } else {
                List<xowl.lang.owl2.LiteralExpression> exps = new ArrayList<xowl.lang.owl2.LiteralExpression>();
                for (int i=0; i!=children.size() - 1; i++) {
                    xowl.lang.owl2.LiteralExpression temp = ExpressionLoader.loadLiteralExp(ontologyLoader, children.get(i), context);
                    if (temp != null) exps.add(temp);
                }
                statement.setLiteralSeq(xowl.interpreter.impl.Sequences.toSequence_Lit(exps));
            }
        }
        statement.setExpression(ExpressionLoader.load(ontologyLoader, children.get(children.size() - 1), context));
        return statement;
    }
    private static xowl.lang.actions.Statement load_Break(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.actions.Break statement = new xowl.lang.actions.Break();
        return statement;
    }
    private static xowl.lang.actions.Statement load_Continue(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.actions.Continue statement = new xowl.lang.actions.Continue();
        return statement;
    }
    private static xowl.lang.actions.Statement load_Invoke(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        return ExpressionLoader.load_Invoke(ontologyLoader, node, context);
    }
    private static xowl.lang.actions.Statement load_Execute(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        return ExpressionLoader.load_Execute(ontologyLoader, node, context);
    }
    private static xowl.lang.actions.Statement load_Block(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.actions.Block statement = new xowl.lang.actions.Block();
        xowl.interpreter.frontends.LexicalContext innerContext = new xowl.interpreter.frontends.LexicalContext(context);
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        for (org.w3c.dom.Node child : children) {
            xowl.lang.actions.Statement temp = load(ontologyLoader, child, innerContext);
            if (temp != null) statement.addContains(temp);
        }
        return statement;
    }
    private static xowl.lang.actions.Statement load_If(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.actions.If statement = new xowl.lang.actions.If();
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        statement.setCondition(ExpressionLoader.load(ontologyLoader, children.get(0), context));
        statement.setBranchTrue(load(ontologyLoader, children.get(1), context));
        if (children.size() > 2)
            statement.setBranchFalse(load(ontologyLoader, children.get(2), context));
        return statement;
    }
    private static xowl.lang.actions.Statement load_For(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.actions.For statement = new xowl.lang.actions.For();
        xowl.interpreter.frontends.LexicalContext innerContext = new xowl.interpreter.frontends.LexicalContext(context);
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        statement.setVariable(VariableLoader.load(children.get(0), innerContext));
        statement.setExpression(ExpressionLoader.load(ontologyLoader, children.get(1), context));
        statement.setCondition(ExpressionLoader.load(ontologyLoader, children.get(2), innerContext));
        statement.setStep((xowl.lang.actions.Assign)load(ontologyLoader, children.get(3), innerContext));
        statement.setContains(load(ontologyLoader, children.get(4), innerContext));
        return statement;
    }
    private static xowl.lang.actions.Statement load_ForEach(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.actions.ForEach statement = new xowl.lang.actions.ForEach();
        xowl.interpreter.frontends.LexicalContext innerContext = new xowl.interpreter.frontends.LexicalContext(context);
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        statement.setVariable(VariableLoader.load(children.get(0), innerContext));
        statement.setArray(ExpressionLoader.loadArrayExp(ontologyLoader, children.get(1), context));
        statement.setContains(load(ontologyLoader, children.get(4), innerContext));
        return statement;
    }
    private static xowl.lang.actions.Statement load_While(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.actions.While statement = new xowl.lang.actions.While();
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        statement.setCondition(ExpressionLoader.load(ontologyLoader, children.get(0), context));
        statement.setContains(load(ontologyLoader, children.get(1), context));
        return statement;
    }
    private static xowl.lang.actions.Statement load_Declare(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.actions.Declare statement = new xowl.lang.actions.Declare();
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        statement.setVariable(VariableLoader.load(children.get(0), context));
        if (children.size() > 1)
            statement.setExpression(ExpressionLoader.load(ontologyLoader, children.get(1), context));
        return statement;
    }
    private static xowl.lang.actions.Statement load_Return(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.actions.Return statement = new xowl.lang.actions.Return();
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        statement.setExpression(ExpressionLoader.load(ontologyLoader, children.get(0), context));
        return statement;
    }
    private static xowl.lang.actions.Statement load_JavaInvokation(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        return ExpressionLoader.load_JavaInvokation(ontologyLoader, node, context);
    }
}
